Array.prototype.included = function(value) {
  var exist = false;
    for(i=0; i<this.length; i++)
      if(this[i].indexOf(value)>=0)
        exist = true;
    return exist;
}

var pageId = '';
var db = {
   s: window.localStorage,
   pages: new Array(),
   init: function(){
    this.getPages();
    window.addEventListener("keyup",function(e){
      setTimeout(function(){
        db.savePage();
      },100);
    });
    document.getElementById('addNote').style.backgroundImage = 'url("'+addNoteImg+'")';
    document.getElementById('addNote').addEventListener('click',function(){
      pageId= db.createId();
      page = document.createElement('div');
      page.innerHTML = db.getDate();
      page.id = 'page';
      page.dataset.pageId=pageId;
      db.clearActives();
      page.setAttribute('class','active');
      var pageNodes = document.querySelectorAll("#page");
      db.pages.push(pageId+'-'+db.getDate());
      db.s.setItem(pageId,'');
      if(pageNodes.length>0)
        document.getElementById('historybox').insertBefore(page,pageNodes[0]);
      else
        document.getElementById('historybox').appendChild(page);
      document.getElementById('historybox').childNodes[0].addEventListener('click',db.onPageChange);
      editor.setValue('');
    });
   }, 
   getPages: function(){
    var tmp = this.s.pages;
    if(tmp!="" && typeof tmp ==="string")
        this.pages = tmp.split(',');
    //set the default page
    if(this.pages.length>0) {
      var lastPage = this.pages[this.pages.length-1];
      var cmp = lastPage.split('-');
      var lastPageId = cmp[0];
      editor.setValue(db.s.getItem(lastPageId));
      pageId = lastPageId;
    }else
      document.getElementById('addNote').click();
    //render
    var page = null,lastPage = null;
    document.getElementById('historybox').innerHTML='';
    for(var i =0;i<this.pages.length;i++){
      cmp = this.pages[i].split('-');
      page = document.createElement('div');
      page.innerHTML = cmp[1];
      page.id = 'page';
      page.dataset.pageId=cmp[0];
      page.addEventListener('click',this.onPageChange);
      if(i==this.pages.length-1)
        page.setAttribute('class','active');
      if(i>0)
        document.getElementById('historybox').insertBefore(page,lastPage);
      else
        document.getElementById('historybox').appendChild(page);
      lastPage = page;
    }
   },
   savePage: function(){
    if(pageId==='')
        pageId = db.createId();
    var doc = db.getDoc();
    if(doc==='')
      return;
    db.s.setItem(pageId,doc);
    if(!db.pages.included(pageId))
      db.pages.push(pageId+'-'+db.getDate());
    db.s.setItem("pages",db.pages.toString());
    console.log("saved to "+pageId);
   },
   createId: function(){
    var plen=32;
    rcode='0123456789abcdefghijklmnopqrstuvwxyz';
    l=rcode.length;
    ccode='';
    for(i=0;i<plen;i++){
        ccode+=rcode.substr(parseInt(l*Math.random()),1);
    }
    return 'ID'+ccode;
   },
   getDoc: function(){
    return editor.getValue();
   },
   getDate: function(){
    var ctd  = new Date();
    return (ctd.getMonth()+1)+'月 '+ctd.getDate()+'日<br>'+ctd.getHours()+":"+ctd.getMinutes()+':'+ctd.getSeconds();
   },
   onPageChange: function(e){
    var clickedPageId = e.target.dataset.pageId;
    if(clickedPageId && typeof db.s.getItem(clickedPageId)!='undefined') {
      pageId = clickedPageId;
      editor.setValue(db.s.getItem(clickedPageId));
      db.clearActives();
      e.target.setAttribute('class','active');
    }
   },
   clearActives: function(){
    var pageNodes = document.querySelectorAll("#page");
      for(var i =0;i<pageNodes.length;i++){
        pageNodes[i].setAttribute('class','');
      }
   },
   requestDelete: function(){
      if(confirm("Are you sure you want to delete this note?"))
        return ;
   },
};
var addNoteImg = '';
setTimeout(function(){db.init();},200);